App.controller('assetController', ['$rootScope', '$scope', '$state', 'Page', function($rootScope, $scope, $state, Page) {

    $scope.grid = {
        add : function() {
            $scope.type = 1;
            $scope.asset = {};
            $scope.modal.setTitle('新增').show();
        },
        update: function(row) {
            $scope.type = 2;
            $scope.asset = row;
            $scope.assetOld = angular.copy(row);
            $scope.modal.setTitle('编辑').show();
        },
        history: function(row) {
            $scope.assetSelected = row;
            Page.ajaxPost('asset/queryType.do', {typeId : row.type}, function(resp) {
                $scope.assetSelected.typeName = resp.data;
            });
            Page.ajaxPost('assetTransfer/query.do', {assetId : $scope.assetSelected.assetId}, function(resp) {
                $scope.assetHistories = resp.data.rows;
            });
            $scope.modalHistory.show();
        },
        save : function() {
            $scope.asset.deptName = $scope.asset.deptId.deptName;
            $scope.asset.deptId = $scope.asset.deptId.deptId;

            $scope.asset.assetOld = $scope.assetOld;

            Page.ajaxPost($scope.asset.assetId ? 'asset/update.do' : 'asset/add.do', $scope.asset, function() {
                $scope.grid.query();
                $scope.grid.cancel();
            });
        },
        column_defs: [{
            target : 13,
            buttons : [
                {
                    name: '异动记录',
                    class: 'btn-success btm-sm rounded',
                    icon: 'fa-list',
                    event: 'history'
                },
                {
                    name: '编辑',
                    class: 'btn-warning btm-sm rounded',
                    icon: 'fa-edit',
                    event: 'update'
                },
                {
                    name: '删除',
                    class: 'btn-danger rounded',
                    icon: 'fa-close',
                    event: function(row) {
                        Page.confirm("确定要删除吗？", function() {
                            Page.ajaxPost('asset/delete.do', row, function() {
                                $scope.grid.query();
                            });
                        });
                    }
                }
            ]
        }]
    };

    $scope.gridHistory = {
        options: {
            paging: false
        }
    };

    /*自定义查询*/
    $scope.query = function () {
        $scope.grid.query();
    };

    /*自定义重置*/
    $scope.reset = function () {
        $scope.type = '';
        $scope.grid.reset();
    };

}]);